import { create } from "zustand";
import { cloneDeep } from "lodash";
import GlobalData, { UserInfo } from "@/interface/globalData";

const initData = {
  userInfo: JSON.parse(localStorage.getItem("user-info") || "{}") as UserInfo,
};

export const useData = create<GlobalData>((set) => ({
  ...cloneDeep(initData),
  setUserInfo: (userInfo: UserInfo) => set(() => ({ userInfo })),
}));

export const getState = (key: string) => {
  return useData<Partial<GlobalData>>((state: any) => state[key]);
};
